package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.lenovo.demo.CityFirstActivity;
import com.example.lenovo.demo.MainActivity;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.HeaderBean;
import com.example.lenovo.demo.model.databean.HomeBean;
import com.example.lenovo.demo.view.CityHotFragment;
import com.example.lenovo.demo.view.HomeFragment;
import com.example.lenovo.demo.view.utils.GvHeightUtil;

import java.util.List;

/**
 * Created by Lenovo on 2017/1/5.
 */

public class HomeMyRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Object> data;
    private Context context;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    public static HomeFragment homeFragment;

    private static final int TYPE_HEADER = 2;
    private static final int TYPE_ITEM = 1;

    public HomeMyRvAdapter(Context context, List<Object> data, FragmentManager manager, HomeFragment homeFragment) {
        this.context = context;
        this.data = data;
        this.manager=manager;
        this.homeFragment=homeFragment;
    }



    @Override
    public int getItemCount() {

        return data==null?0:data.size();
    }

    /**
     * 根据viewType返回不同viewHolder
     * */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_ITEM){
            final View view = LayoutInflater.from(context).inflate(R.layout.home_myrv_item, parent, false);
            return new ViewHolder(view);
        }else {
            final View view = LayoutInflater.from(context).inflate(R.layout.home_header, parent, false);
            return new ViewHolderHeader(view);
        }
    }

    /**
     * 多布局的视图展示
     * 头部视图的二级跳转
     * item项的二级跳转
     * 旅游攻略btn的二级跳转
     * */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position!=0){
            ViewHolder viewHolder= (ViewHolder) holder;
            final HomeBean.DataBean dataBean= (HomeBean.DataBean) data.get(position);
            viewHolder.tv1.setText(dataBean.getName());
            viewHolder.tv2.setText(dataBean.getButton_text());
            //旅游攻略btn的二级跳转
            viewHolder.tv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    transaction =manager.beginTransaction();
                    Bundle bundle=new Bundle();
                    bundle.putString("msg",dataBean.getRegion());
                    CityHotFragment fragment=new CityHotFragment();
                    fragment.setArguments(bundle);
                    transaction.add(R.id.main_drawerlayout_right,fragment)
                               .commit();

                }
            });
            Home_GvAdapter adapter=new Home_GvAdapter(dataBean.getDestinations(),context);
            viewHolder.gridView.setAdapter(adapter);
            GvHeightUtil.setGirdViewHeightBasedOnChildren(viewHolder.gridView,3);
            //item项的二级跳转
            viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent=new Intent(context, CityFirstActivity.class);
                    intent.putExtra("msg",dataBean.getDestinations().get(i).getId()+"");
                    context.startActivity(intent);
                }
            });
            /**
             * 加载头部视图,用的第三方库
             compile 'com.daimajia.slider:library:1.1.5@aar'
             compile 'com.nineoldandroids:library:2.4.0'
             * */
        }else {
                //头部视图的二级跳转
                ViewHolderHeader viewHolderHeader= (ViewHolderHeader) holder;
                List<HeaderBean.DataBean> datas= (List<HeaderBean.DataBean>) data.get(position);
                for (int i = 0; i < datas.size(); i++) {
                String mainTitle=datas.get(i).getTopic();
                TextSliderView textSliderView=new TextSliderView(context);
                textSliderView.image(datas.get(i).getPhoto().getPhoto_url());
                textSliderView.description(mainTitle);
                viewHolderHeader.sliderLayout.addSlider(textSliderView);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        //第一条为头部视图
        if (position==0) {
            return TYPE_HEADER;
        }
        return TYPE_ITEM;
    }
    /**
     * item的ViewHolder
     * */
    public class ViewHolder extends RecyclerView.ViewHolder {
        GridView gridView;
        TextView tv1,tv2;
        public ViewHolder(View itemView) {
            super(itemView);
            tv1= (TextView) itemView.findViewById(R.id.home_title_tv);
            tv2= (TextView) itemView.findViewById(R.id.home_title_tv2);
            gridView= (GridView) itemView.findViewById(R.id.home_title_gv);
        }
    }
    /**
     * 头部视图的ViewHolder
     * */
    public class ViewHolderHeader extends  RecyclerView.ViewHolder{
        SliderLayout sliderLayout;
        public ViewHolderHeader(View itemView) {
            super(itemView);
            sliderLayout= (SliderLayout) itemView.findViewById(R.id.header_slider);
        }
    }
}
